package es.ats.codington.Spring.Controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.services.*;
import es.ats.codington.festival.domain.*;

/**
 * The Class EventSignUpController.
 */
@Controller
public class EventSignUpController {
	
	/**
	 * Register event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 * @throws Exception the exception
	 */
	@RequestMapping(value = "/registerEventSignUp.htm")
	public String registerEventController(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try{		
			//HttpSession session = request.getSession(true);
			int IdEvent = Integer.parseInt(request.getParameter("Id"));

			 Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
			 int IdVisitor = visitor.getVisitorId();
			 
			
				//EventSignUp eventSignUp = new EventSignUp(IdEvent, IdVisitor);
				
				EventSingUpService eventSignUpService = new EventSingUpService();
				
				//This order access to the DB
				eventSignUpService.registerInEvent(IdVisitor, IdEvent);

				//Actualize the arrays 
				return "redirect:/list_event.htm";
				//return "list_event.htm";


		}catch(Exception e){
			request.setAttribute("error", "Login incorrect.");
			return "redirect:/list_event.htm";
		}
		
		
	}

	/**
	 * Unregister event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the string
	 */
	@RequestMapping(value = "/unregisterEvent.htm")
	public String unRegisterEventController(HttpServletRequest request, HttpServletResponse response) {
				try{
					
				//	HttpSession session = request.getSession(true);
					int IdEvent = Integer.parseInt(request.getParameter("Id"));
					 Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
					 int IdVisitor = visitor.getVisitorId();
					 
						//EventSignUp eventSignUp = new EventSignUp(IdEvent, IdVisitor);
						
						EventSingUpService eventSignUpService = new EventSingUpService();
						
						//This order access to the DB
						eventSignUpService.unRegisterInEvent(IdVisitor, IdEvent);

						//Actualize the arrays 
						return "redirect:/list_event.htm";


						
				}catch(Exception e){
					request.setAttribute("error", "Login incorrect.");
					return "login";
				}

			}
	
	
	/**
	 * Actualize arrays to send jsp.
	 *
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping(value = "/list_event.htm")
	public ModelAndView actualizeArraysToSendJsp(HttpServletRequest request){
		 HttpSession session = request.getSession(true);
		 Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
		 int IdVisitor = visitor.getVisitorId();
		 ModelAndView model = new ModelAndView();
		 try{		
				/**
				 * Your Events
				 */
				
				/* Prueba */
				EventSingUpService ev = new EventSingUpService();				
				ArrayList<Event> event = new ArrayList<Event>();
				boolean result = ev.visitorInEvent(IdVisitor, event);
				
				/**
				 * All Events
				 */
				EventSingUpService ev2 = new EventSingUpService();					
				ArrayList<Event> event2 = new ArrayList<Event>();
			boolean result2 = ev.obtainSemiAllEvents(IdVisitor, event2);
				
				model.addObject("YourEvents", event);
				model.addObject("SemiAllEvents", event2);
				model.setViewName("listEventVisitor");
			
					
		    }catch(Exception theException){
		        System.out.println(theException); 
		   }
		return model;
					
	}
	
}

